<template>
  <div class='container'>
    <el-card>
      <div slot="header">
        <el-row type="flex" justify="space-between">
          <div>
            <span class="title">学科名称</span>
            
            <el-input style="width:190px;padding:0px 15px" v-model="searchIpt" placeholder=""></el-input>
            
            <el-button size="small" @click="onClear" >清除</el-button>
            <el-button type="primary" size="small" @click="onSearch">搜索</el-button>
          </div>
        <el-button type="success" size="small" @click="subjectsAddShow = true">
          <i class="el-icon-edit"></i>
          新增学科
        </el-button>
      </el-row>
    </div>

      <div class="gantanhao">
        <i class="el-alert__icon el-icon-info"></i>
        <span class="el-alert__title">数据一共 {{page.counts}} 条</span>
      </div>
     
     <!--表格区域  -->
     <el-table
      :data="subjectsList"
      style="width: 100%; margin-top: 7px;  backgroundColor: #fafafa;"
      >
      <el-table-column
        label="序号"
        type="index" 
        width="width">
      </el-table-column>
      <el-table-column
      prop="subjectName"
        label="学科名称"
        width="100">
      </el-table-column>
      <el-table-column
        property="username"
        label="创建者"
        width="width">
      </el-table-column>
      <el-table-column
        property="addDate"
        label="创建日期"
        width="width">
        <template v-slot="{row}">
          {{ row.addDate | parseTimeByString("{y}-{m}-{d} {h}:{i}:{s}")}}
        </template>
      </el-table-column>
      <el-table-column
        property="isFrontDisplay"
        label="前台是否显示"
        width="width">
        <template v-slot="{row}">
          {{ row.isFrontDisplay ?'是':'否' }}
        </template>
      </el-table-column>
      <el-table-column
        property="twoLevelDirectory"
        label="二级目录"
        width="width">
      </el-table-column>
      <el-table-column
        property="tags"
        label="标签"
        width="width">
      </el-table-column>
      <el-table-column
        property="totals"
        label="题目数量"
        width="width">
      </el-table-column>
      <el-table-column
        label="操作"
        width="230"
        class="caozuo"
        >
        <template v-slot="{row}">
           <el-button type="text"  @click="jumpToSubject(row)" >学科分类</el-button>
           <el-button type="text"  @click="jumpToTag(row)">学科标签</el-button>
          
            <el-button type="text" @click="onEdit(row.id)">修改</el-button>
            <el-button type="text" @click="onDel(row.id)">删除</el-button>
          </template>
      </el-table-column>
      
     </el-table> 

       <!-- 页码 -->
      <el-row type="flex" justify="end" style="margin-top:15px;">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.page"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="page.pagesize"
          layout="prev, pager, next, sizes, jumper"
          :total="page.counts"
          >
        </el-pagination>
        
          <!-- <el-pagination
            layout="prev, pager, next"
            :current-page="page.page"
            :total="page.counts"
            :size="page.pagesize"
            @current-change="onCurrentChange"
          >
          </el-pagination> -->
      </el-row>
    </el-card>

    <!-- 新增学科 -->
    <subjects-add ref="subjectsAdd" :subjectsAddShow.sync="subjectsAddShow"  @success="getSubjects"></subjects-add>

  
  </div>
</template>

<script>
import SubjectsAdd from '../components/subjects-add.vue'
import { list, update, simple, detail, remove } from '../../api/hmmm/subjects.js'
export default {
  name: 'SubjectPage',
  components: {
    SubjectsAdd    
  },
  data () {
    return {
      subjectsList: [],
      subjectsAddShow:false,
      page: {
        page: 1, // 当前页码
        pagesize: 10, // 当前条数
        counts: 0,
      },
      searchIpt:''
    }
  },
  created() {
    this.getSubjects()
    this.onSearch()
    this.simple ()
  },
  methods: {
    async simple () {
      const res = await simple(this.subjectsList)
      console.log(res)
    },
    jumpToSubject (data) {
      this.$router.push({ name: 'subjects-directorys', query: { id: data.id, name: data.subjectName } })
    },
    jumpToTag (data) {
      this.$router.push({ name: 'subjects-tags', query: { id: data.id, name: data.subjectName } })
    },
    onDel(id) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async action => {
        await remove({id})
        this.$message.success('操作成功')
        this.getSubjects()
      }).catch(() => {
      })
    },
    async onEdit(id) {
      this.subjectsAddShow = true
      // 数据回显
      const res = await detail({id})
      console.log(res)
      this.$refs.subjectsAdd.form= res.data
    },
    async getSubjects () {
      const res = await list({
          ...this.page,
          subjectName: this.searchIpt
        })
        console.log(res)
        this.subjectsList = res.data.items
        this.page.counts = res.data.counts
      },
    onClear () {
      this.searchIpt='',
      this.onSearch()
    },
    onSearch () {
        this.page.page = 1,
        this.getSubjects()
    },
      // 实现分页功能
    handleCurrentChange(val) {
      this.page.page = val
      this.getSubjects()
    },
    handleSizeChange(val) {
      this.page.pagesize = val
      this.getSubjects()
    },
  },
}
</script>

<style scoped lang='less'>
.container{
  .title{
    width: 80px;
    font-size:14px;
    color: #606266;
    font-weight: 700;
    padding: 0 8px 0 5px;
  }
  .gantanhao{
    height: 35px;
    background-color: #f4f4f5;
    color: #909399;
    padding: 8px 16px;
    border-radius: 4px;
    transition: opacity .2s;
    .el-alert__title{
      margin-left: 8px;
    }
  }
  .caozuo{
    width: 300px;
    padding: 10px 0px;
  }
  .el-table th.is-leaf {
    border-bottom: 2px solid #e8e8e8;
  }
}
</style>
